package rexsee.content;

import rexsee.core.browser.Browser;
import rexsee.core.browser.clazz.JavascriptInterface;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;

public class RexseeAccounts implements JavascriptInterface {

	public static final String INTERFACE_NAME = "Accounts";
	@Override
	public String getInterfaceName() {
		return mBrowser.application.resources.prefix + INTERFACE_NAME;
	}
	@Override
	public JavascriptInterface getInheritInterface(Browser childBrowser) {
		return this;
	}
	@Override
	public JavascriptInterface getNewInterface(Browser childBrowser) {
		return new RexseeAccounts(childBrowser);
	}

	private final Browser mBrowser;
	private final Context mContext;

	public RexseeAccounts(Browser browser) {

		mBrowser = browser;
		mContext = browser.getContext();

	}
	public RexseeAccounts(Context context) {
		mBrowser = null;
		mContext = context;
	}

	//JavaScript Interface
	public String get() {
		String rtn = "";
		AccountManager accountManager = AccountManager.get(mContext);
		Account[] accounts = accountManager.getAccounts();
		for (int i = 0; i < accounts.length; i++) {
			if (i > 0) rtn += ",";
			rtn += "{";
			rtn += "\"name\":\"" + accounts[i].name + "\"";
			rtn += ",\"type\":\"" + accounts[i].type + "\"";
			rtn += "}";
		}
		rtn = "[" + rtn + "]";
		return rtn;
	}

}
